﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lyh.XianGuo.Reader.Common;
using Lyh.XianGuo.Reader.Model;
using Lyh.XianGuo.Reader.Properties;

namespace Lyh.XianGuo.Reader
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void menuDownload_Click(object sender, EventArgs e)
        {
            using (var frm=new DownloadForm())
            {
                frm.ShowDialog();
                BindTreeView();
            }
        }

        private void BindTreeView()
        {
            tvMenu.Nodes.Clear();
            var folder = string.Format("{0}/Books", Environment.CurrentDirectory);
            var dir=new DirectoryInfo(folder);
            foreach (var info in dir.GetDirectories())
            {
                var file = string.Format("{0}/{1}.json", info.FullName, info.Name);
                var json = File.ReadAllText(file, Encoding.UTF8);
                var book = JsonConverter.FromJson<Book>(json);

                var nodeBook = new TreeNode(book.Name) {Tag = book, ImageIndex = 0};
                foreach (var chapter in book.Chapters)
                {
                    var nodeChapter = new TreeNode(chapter.Name) {Tag = chapter, ImageIndex = 1};
                    foreach (var page in chapter.Pages)
                    {
                        var nodePage = new TreeNode(page.Name) {Tag = page, ImageIndex = 2, SelectedImageIndex = 3};
                        nodeChapter.Nodes.Add(nodePage);
                    }
                    nodeBook.Nodes.Add(nodeChapter);
                }
                tvMenu.Nodes.Add(nodeBook);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            CreateCss();
            BindTreeView();
        }

        private static void CreateCss()
        {
            var folder = string.Format("{0}\\Books", Environment.CurrentDirectory);
            if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
            var path = string.Format("{0}\\Books\\reader.css", Environment.CurrentDirectory);
            File.WriteAllText(path, Resources.css, Encoding.UTF8);
        }

        private void tvMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
        }

        private void menuRefresh_Click(object sender, EventArgs e)
        {
            BindTreeView();
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void menuDelete_Click(object sender, EventArgs e)
        {
            var node = tvMenu.SelectedNode;
            if (node == null)
            {
                MessageBox.Show("请选择要删除的图书");
                return;
            }
            var book = node.Tag as Book;
            if (book == null) return;

            if (MessageBox.Show(string.Format("确定要删除《{0}》？", book.Name), "确认", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk) != DialogResult.OK) return;
            var folder = string.Format("{0}/Books/{1}", Environment.CurrentDirectory, book.Id);
            Directory.Delete(folder, true);
            BindTreeView();
            MessageBox.Show("删除成功");
        }

        private void tvMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var page = e.Node.Tag as Page;
            if (page == null) return;
            var book = e.Node.Parent.Parent.Tag as Book;
            if (book == null) return;
            var url = string.Format("{0}/Books/{1}/{2}.html", Environment.CurrentDirectory, book.Id, page.Id);
            webBrowser.Url = new Uri(url);
        }
    }
}
